package com.yipao.db.domain.order;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * 订单商品
 *
 * @author tianhao
 */
@Data
@EqualsAndHashCode(callSuper = false)
@ToString
public class OrderGoods {
    private Integer id;
    /**
     * 订单表的订单ID
     */
    private Integer orderId;
    /**
     * 商品表的商品ID
     */
    private Integer goodsId;
    /**
     * 商品名称
     */
    private String goodsName;
    /**
     * 商品编号
     */
    private String goodsSn;
    /**
     * 商品货品表的货品ID
     */
    private Integer productId;
    /**
     * 商品货品的购买数量
     */
    private Short number;
    /**
     * 商品货品的售价
     */
    private BigDecimal price;
    /**
     * 商品货品的规格列表
     */
    private String[] specifications;
    /**
     * 商品货品图片或者商品图片
     */
    private String picUrl;
    /**
     * 订单商品评论，如果是-1，则超期不能评价；如果是0，则可以评价；如果其他值，则是comment表里面的评论ID。
     */
    private Integer comment;
    /**
     * 原料id
     */
    private Integer formulaId;
    /**
     * 配方属性json
     */
    private String formulaAttrJson;
    /**
     * 口味,多个配方属性名称,多个用/隔开
     * 示例: 标准/热饮/冷饮
     */
    private String taste;
    /**
     * 创建时间
     */
    private LocalDateTime addTime;
    /**
     * 更新时间
     */
    private LocalDateTime updateTime;
    /**
     * 逻辑删除
     */
    private Integer deleted;


    public enum Column {
        id("id", "id", "INTEGER", false),
        orderId("order_id", "orderId", "INTEGER", false),
        goodsId("goods_id", "goodsId", "INTEGER", false),
        goodsName("goods_name", "goodsName", "VARCHAR", false),
        goodsSn("goods_sn", "goodsSn", "VARCHAR", false),
        productId("product_id", "productId", "INTEGER", false),
        number("number", "number", "SMALLINT", true),
        price("price", "price", "DECIMAL", false),
        specifications("specifications", "specifications", "VARCHAR", false),
        picUrl("pic_url", "picUrl", "VARCHAR", false),
        comment("comment", "comment", "INTEGER", true),
        addTime("add_time", "addTime", "TIMESTAMP", false),
        updateTime("update_time", "updateTime", "TIMESTAMP", false),
        deleted("deleted", "deleted", "BIT", false);

        private static final String BEGINNING_DELIMITER = "`";

        private static final String ENDING_DELIMITER = "`";

        private final String column;

        private final boolean isColumnNameDelimited;

        private final String javaProperty;

        private final String jdbcType;


        public String value() {
            return this.column;
        }


        public String getValue() {
            return this.column;
        }


        public String getJavaProperty() {
            return this.javaProperty;
        }


        public String getJdbcType() {
            return this.jdbcType;
        }


        Column(String column, String javaProperty, String jdbcType, boolean isColumnNameDelimited) {
            this.column = column;
            this.javaProperty = javaProperty;
            this.jdbcType = jdbcType;
            this.isColumnNameDelimited = isColumnNameDelimited;
        }


        public String desc() {
            return this.getEscapedColumnName() + " DESC";
        }


        public String asc() {
            return this.getEscapedColumnName() + " ASC";
        }


        public static Column[] excludes(Column... excludes) {
            ArrayList<Column> columns = new ArrayList<>(Arrays.asList(Column.values()));
            if (excludes != null && excludes.length > 0) {
                columns.removeAll(new ArrayList<>(Arrays.asList(excludes)));
            }
            return columns.toArray(new Column[]{});
        }


        public String getEscapedColumnName() {
            if (this.isColumnNameDelimited) {
                return new StringBuilder().append(BEGINNING_DELIMITER).append(this.column).append(ENDING_DELIMITER).toString();
            } else {
                return this.column;
            }
        }


        public String getAliasedEscapedColumnName() {
            return this.getEscapedColumnName();
        }
    }
}
